// 04.03.12 - Matthew Brews - Original Creation.

#include "panel.h"

CPanel::CPanel()
    : m_pParent(nullptr),
    m_fHeight(static_cast<float>(Constants::WINDOW_HEIGHT)),
    m_fWidth(static_cast<float>(Constants::WINDOW_WIDTH)),
    m_bVisible(true)
{
    setColour(0.0f, 0.0f, 0.0f, 255.0f);
}

CPanel::~CPanel()
{
    if (m_arrChildren.size() > 0)
    {
        for (map<string, CPanel*>::const_iterator pPanel(m_arrChildren.begin());
                pPanel != m_arrChildren.end();)
        {
            CPanel *pPanelRef(pPanel->second);
            pPanel = m_arrChildren.erase(pPanel);
            delete pPanelRef;
        }
        m_arrChildren.clear();
    }
}

void CPanel::removeChild(CPanel *pChild)
{
    if (!m_arrChildren.size())
    {
        return;
    }
    for (map<string, CPanel*>::const_iterator pPanel(m_arrChildren.begin());
            pPanel != m_arrChildren.end();)
    {
        if (pPanel->second == pChild)
        {
            delete pPanel->second;
            pPanel = m_arrChildren.erase(pPanel);
        }
        else
        {
            ++pPanel;
        }
    }
}

float CPanel::getMarginLeft() const
{
    return m_margin.getLeft();
}

float CPanel::getMarginTop() const
{
    return m_margin.getTop();
}

float CPanel::getMarginRight() const
{
    return m_margin.getRight();
}

float CPanel::getMarginBottom() const
{
    return m_margin.getBottom();
}

void CPanel::setMarginLeft(float left)
{
    m_margin.setLeft(left);
}

void CPanel::setMarginTop(float top)
{
    m_margin.setTop(top);
}

void CPanel::setMarginRight(float right)
{
    m_margin.setRight(right);
}

void CPanel::setMarginBottom(float bottom)
{
    m_margin.setBottom(bottom);
}

void CPanel::setMargin(float left, float top, float right, float bottom)
{
    m_margin.setLeft(left);
    m_margin.setTop(top);
    m_margin.setRight(right);
    m_margin.setBottom(bottom);
}

void CPanel::setPaddingLeft(float left)
{
    m_padding.setLeft(left);
}

void CPanel::setPaddingTop(float top)
{
    m_padding.setTop(top);
}

void CPanel::setPaddingRight(float right)
{
    m_padding.setRight(right);
}

void CPanel::setPaddingBottom(float bottom)
{
    m_padding.setBottom(bottom);
}

void CPanel::setPadding(float left, float top, float right, float bottom)
{
    m_padding.setLeft(left);
    m_padding.setTop(top);
    m_padding.setRight(right);
    m_padding.setBottom(bottom);
}

float CPanel::getPaddingLeft() const
{
    return m_padding.getLeft();
}

float CPanel::getPaddingTop() const
{
    return m_padding.getTop();
}

float CPanel::getPaddingRight() const
{
    return m_padding.getRight();
}

float CPanel::getPaddingBottom() const
{
    return m_padding.getBottom();
}

CPanel *CPanel::getParent() const
{
    return m_pParent;
}

void CPanel::setParent(CPanel *pPanel)
{
    m_pParent = pPanel;
}

void CPanel::addChild(const string &szReferenceName, CPanel *pChild)
{
    m_arrChildren.insert(pair<string, CPanel*>(szReferenceName, pChild));
    pChild->setParent(this);
}

const map<string, CPanel*> &CPanel::getChildren() const
{
    return m_arrChildren;
}

int CPanel::getChildrenCount() const
{
     return m_arrChildren.size();
}

CPanel *CPanel::getChild(const string &szRefrenceName) const
{
    map<string, CPanel*>::const_iterator pPosition(
        m_arrChildren.find(szRefrenceName));
    if (pPosition == m_arrChildren.end())
    {
        return nullptr;
    }
    return pPosition->second;
}

float CPanel::getPositionLeft() const
{
    float fLeft(0);
    if (m_pParent != nullptr)
    {
        fLeft = m_pParent->getPositionLeft();
    }
    fLeft += getMarginLeft() + getPaddingLeft();
    return fLeft;
}

float CPanel::getPositionTop() const
{
    float fTop(0);
    if (m_pParent != nullptr)
    {
        fTop = m_pParent->getPositionTop();
    }
    fTop += getMarginTop() + getPaddingTop();
    return fTop;
}

float CPanel::getPositionRight() const
{
    return m_position.getRight();
}

float CPanel::getPositionBottom() const
{
    return m_position.getBottom();
}

void CPanel::setPosition(float left, float top, float right, float bottom)
{
    m_margin.setLeft(left);
    m_margin.setTop(top);
    m_margin.setRight(right);
    m_margin.setBottom(bottom);
}

void CPanel::setColour(D3DCOLORVALUE colour)
{
    m_colour = colour;
}

void CPanel::setColour(float red, float green, float blue)
{
    m_colour.r = red;
    m_colour.g = green;
    m_colour.b = blue;
}

void CPanel::setColour(float red, float green, float blue, float alpha)
{
    m_colour.r = red;
    m_colour.g = green;
    m_colour.b = blue;
    m_colour.a = alpha;
}

float CPanel::getRed() const
{
    return m_colour.r;
}

float CPanel::getGreen() const
{
    return m_colour.g;
}

float CPanel::getBlue() const
{
    return m_colour.b;
}

float CPanel::getAlpha() const
{
    return m_colour.a;
}

D3DCOLORVALUE CPanel::getColour() const
{
    return m_colour;
}

float CPanel::getHeight() const
{
    return m_fHeight;
}

float CPanel::getWidth() const
{
    return m_fWidth;
}

void CPanel::setHeight(float height)
{
    m_fHeight = height;
}

void CPanel::setWidth(float width)
{
    m_fWidth = width;
}

void CPanel::setSize(float height, float width)
{
    m_fHeight = height;
    m_fWidth = width;
}

void CPanel::hide()
{
    m_bVisible = false;
}

void CPanel::show()
{
    m_bVisible = true;
}

bool CPanel::isVisible() const
{
    return m_bVisible;
}

void CPanel::render()
{
    if (isVisible())
    {
        renderChildren();
    }
}

void CPanel::renderChildren()
{
    for (map<string, CPanel*>::const_iterator pPanel(m_arrChildren.begin());
            pPanel != m_arrChildren.end(); ++pPanel)
    {
        pPanel->second->render();
    }
}